package vn.smartoffice.promail.config;

import static vn.smartoffice.actor.SpringExtension.SpringExtProvider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;

@Configuration
public class ActorConfig {

	// the application context is needed to initialize the Akka Spring Extension
	@Autowired
	private ApplicationContext applicationContext;

	/**
	 * Actor system singleton for this application.
	 */
	@Bean
	public ActorSystem actorSystem() {
		ActorSystem system = ActorSystem.create("AkkaJavaSpring");
		// initialize the application context in the Akka Spring Extension
		SpringExtProvider.get(system).initialize(applicationContext);
		return system;
	}

	@Bean(name = "emailActor")
	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
	public ActorRef emailActor(ActorSystem actorSystem) {
		return actorSystem.actorOf(SpringExtProvider.get(actorSystem).props("emailActorBean"));
	}
}
